/**
 * 
 */
package com.iwords.web.action;

import java.io.IOException;
import java.util.List;

import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONException;

import com.iwords.constants.Constants;
import com.iwords.enums.KnownLevel;
import com.iwords.enums.StudyCodeStatus;
import com.iwords.enums.SysLibLevel;
import com.iwords.service.LearningStatusServiceLocal;
import com.iwords.service.SysLibServiceLocal;
import com.iwords.service.UserServiceLocal;
import com.iwords.service.object.LearningStatusEntity;
import com.iwords.service.object.SysLibEntity;
import com.iwords.web.base.BaseAction;
import com.iwords.web.base.BaseAjaxAction;

/**
 * @author       BOLIN.HBC 
 * @time         2011-3-13    
 * @description  系统课本-SERVLET        
 */
@WebServlet(name = "commonAjax", urlPatterns = { "/ajaxApi"})
public class CommonAjaxAction extends BaseAjaxAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@EJB
	SysLibServiceLocal sysLibService;
	
	@EJB
	LearningStatusServiceLocal learningStatusService;

	@EJB
	UserServiceLocal userService;

	public void doProcess(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException, JSONException {

		String action = req.getParameter("action");

		if (Constants.SYSLIB_CHILDREN_ACTION.equals(action)) {
			doGetChildrenSysLib(req, res);

		}
	}

	public void doGetChildrenSysLib(HttpServletRequest req, HttpServletResponse res) throws JSONException, IOException {

		long libid = Long.parseLong(req.getParameter("libid"));
		SysLibEntity parentLib = sysLibService.findSysLibById(libid);
		
		SysLibLevel parentLevel = SysLibLevel.getLevelByValue(parentLib.getLiblevel());
		
		if(parentLevel == null || parentLevel.isLeaf()){
			this.sendRedirect(res, Constants.ERROR_PAGE_JSP, "参数异常");
		}
		
		List<SysLibEntity> libList = sysLibService.getChildrenList(libid);
		initSysLib(libList, this.getUserId(req));
		
		
		SysLibLevel currentLevel = parentLevel.getNextLevel();
		
		req.setAttribute("parentlibid", libid);
		req.setAttribute("isRoot", parentLevel.isRoot());
		req.setAttribute("isLeaf",currentLevel.isLeaf());
		req.setAttribute("sysLibList", libList);
		
		this.forward(req, res, Constants.SHOW_LIBS);
	}

	private void initSysLib(List<SysLibEntity> entitys, Long userid) {

		for (SysLibEntity sys : entitys) {
			LearningStatusEntity l = new LearningStatusEntity();
			l.setUserid(userid);
			l.setLibid(sys.getLibid());

			if (learningStatusService == null) {
				sys.setStudyCode(StudyCodeStatus.UNKNOWN.getStatus());

			} else {
				if (learningStatusService.isExistUserLib(l)) {
					if (sysLibService.getStudyLibNum(userid, sys.getLibid(), KnownLevel.Known.getValue()) == sys.getQuantity().longValue()) {
						sys.setStudyCode(StudyCodeStatus.END.getStatus());
					} else {
						sys.setStudyCode(StudyCodeStatus.KNOWN.getStatus());
					}
				} else {
					sys.setStudyCode(StudyCodeStatus.UNKNOWN.getStatus());
				}
			}
		}

	}

}
